#include <stdio.h>
#include <set>
#include <queue>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 55;
const int M = 3000;

typedef struct Node
{
    int x, y, w;
} Node;
struct str
{
    int x, y, w;
} a[M << 2];
char s[N][N];
int map[N][N], vis[N][N], fa[M];
int n, m, cnt;
int op[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

void bfs(int x, int y)
{
    memset(vis, 0, sizeof vis);
    queue<Node> que;
    Node p, q;
    p.x = x;
    p.y = y;
    p.w = 0;       // 到本身的距离的为0
    vis[x][y] = 1; // 标记已经走过了
    que.push(p);   // 存入到队列中
    while (!que.empty())
    {
        p = que.front(); // 从队列中取出来
        que.pop();
        for (int i = 0; i < 4; i++)
        {
            q.x = op[i][0] + p.x;
            q.y = op[i][1] + p.y;
            q.w = p.w + 1; // 距离也要统计一下
            // 寻找在范围内并且没有走过的点，还是可以走的通的
            if (q.x >= 1 && q.x <= n && q.y >= 1 && q.y <= m && vis[q.x][q.y] == 0 && map[q.x][q.y] >= 0)
            {
                vis[q.x][q.y] = 1; // 标记这个点已经走过了
                que.push(q);
                if (map[q.x][q.y] > 0)
                { // 如果满足是另一个点，则把两点的距离记录下来
                    a[cnt].x = map[x][y];
                    a[cnt].y = map[q.x][q.y];
                    a[cnt++].w = q.w;
                }
            }
        }
    }
    return;
}
bool cmp(struct str a, struct str b)
{
    return a.w < b.w;
}
int Find(int x)
{
    if (fa[x] == x)
        return x;
    else
        return fa[x] = Find(fa[x]);
}
int kruskal()
{
    for (int i = 0; i < M; i++)
        fa[i] = i;
    sort(a, a + cnt, cmp);
    int ans = 0, i = 0;
    while (i < cnt)
    {
        int fx = Find(a[i].x);
        int fy = Find(a[i].y);
        if (fx != fy)
        {
            fa[fx] = fy;
            ans += a[i].w;
        }
        i++;
    }
    return ans;
}
void solve()
{
    memset(map, 0, sizeof map);
    int num = 0;
    cnt = 0;
    scanf("%d%d", &m, &n);
    gets(s[0]);
    for (int i = 1; i <= n; i++)
    {
        gets(s[i] + 1);
        for (int j = 1; j <= m; j++)
        {
            // 表示>=0的就是可以走的通的
            if (s[i][j] == 'A' || s[i][j] == 'S')
            {
                map[i][j] = ++num;
            }
            else if (s[i][j] == '#')
            {
                map[i][j] = -1;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (map[i][j] > 0)
            {
                bfs(i, j); // 用bfs计算这个点到其他点的距离
            }
        }
    }
    printf("%d\n", kruskal());
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        solve();
    }
    return 0;
}